{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# try_except异常处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "try:\n",
    "    正常的操作\n",
    "   ......................\n",
    "except:\n",
    "    发生异常，执行这块代码\n",
    "   ......................\n",
    "else:\n",
    "    如果没有异常执行这块代码\n",
    "finally:\n",
    "    不管有没有异常都执行这块代码\n",
    "   ......................"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-24T09:42:32.084000Z",
     "start_time": "2021-11-24T09:42:32.078000Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['罗平鲁布革多依河景区一目十滩-L1800\\n', '富源大河文轩路大河文化站-L1800\\n', '富源墨红朝阳街8号-L1800\\n', '富源十八连山雨旺山山红酒店楼顶-L1800\\n', '陆良板桥郭地堡村西边-L1800\\n', '宣威热水关营村委会索索卡村-L1800\\n', '沾益炎方珠江源风景区内停车场西侧-L1800\\n', '陆良大莫古阿油堡村北-L1800\\n', '麒麟沿江街道新圩村委会新圩北村127号-L1800\\n', '宣威板桥东屯水库-L1800\\n']\n",
      "另外....\n",
      "文件已关闭！\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    txt = open('./test.txt','r',encoding = 'utf-8')\n",
    "    content = txt.readlines()\n",
    "    print(content[0:10])\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "    print('没有找到文件，或文件打开失败！')\n",
    "else:\n",
    "    print('文件读取成功！')\n",
    "    txt.close()\n",
    "finally:\n",
    "    print('流程结束！')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
